package com.ibeeking.found.upms.s.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.GlobalConstant;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.s.rest.service.IShopPermissionService;
import com.ibeeking.found.upms.s.rest.service.IShopRolePermissionService;
import com.ibeeking.found.upms.s.rest.service.IShopRoleService;
import com.ibeeking.found.upms.service.common.bo.ShopRolePermissionBO;
import com.ibeeking.found.upms.service.common.vo.CurrentPermissionVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.TreeFastUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName PermissionController控制器
 * @Description
 * @Author ibeeking
 * @Date 2020-12-01 18:15
 **/
@RestController
@RequestMapping("/b/permission/v1")
public class ShopPermissionController {

    @Resource
    private IShopPermissionService shopPermissionService;
    @Resource
    private IShopRolePermissionService shopRolePermissionService;
    @Resource
    private IShopRoleService shopRoleService;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询有效权限菜单树")
    @GetMapping(value = "/queryEffectiveList")
    public List<CurrentPermissionVO> queryEffectiveList(@LoginUser(isFull = true) CurrentUser currentUser, HttpServletRequest request) {
        List<CurrentPermissionVO> currentPermissionVos = shopPermissionService.queryEffectiveList(currentUser, request.getHeader(HeaderNounConstant.JWT_JTI));
        if (Collections3.isEmpty(currentPermissionVos)) {
            return Collections3.emptyList();
        }
        return TreeFastUtils.listToTree(currentPermissionVos, GlobalConstant.PARENT_ID);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "根据权限id查询角色id列表")
    @GetMapping(value = "/queryRoleIdsByPermissionId/{permissionId}")
    public List<String> queryRoleIdsByPermissionId(@PathVariable("permissionId") @NotNull(message = "permissionId不能为空") Long permissionId) {
        List<ShopRolePermissionBO> rolePermissionBos = shopRolePermissionService.queryRolePermissionByPermissionId(permissionId);
        List<String> result = rolePermissionBos.parallelStream().map(rolePermissionBO -> String.valueOf(rolePermissionBO.getRoleId())).distinct().collect(Collectors.toList());
        result.add(String.valueOf(shopRoleService.selectAdministratorId()));
        return result;
    }
}
